package com.evaluation.kexingcp.psycho.evaluation.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.evaluation.kexingcp.common.core.util.R;
import com.evaluation.kexingcp.common.excel.annotation.ResponseExcel;
import com.evaluation.kexingcp.common.log.annotation.SysLog;
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationFactorEntity;
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationFactorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 量表因子结果表
 *
 * @author pig
 * @date 2024-07-02 15:32:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/evaluationFactor" )
@Tag(description = "evaluationFactor" , name = "量表因子结果表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EvaluationFactorController {

    private final  EvaluationFactorService evaluationFactorService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param evaluationFactor 量表因子结果表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_view')" )
    public R getEvaluationFactorPage(@ParameterObject Page page, @ParameterObject EvaluationFactorEntity evaluationFactor) {
        if (StrUtil.isEmpty(evaluationFactor.getScaleId())) {
            page.setRecords(null);
            return R.ok(page);
        }

        LambdaQueryWrapper<EvaluationFactorEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(evaluationFactor.getScaleId()), EvaluationFactorEntity::getScaleId,evaluationFactor.getScaleId());
        return R.ok(evaluationFactorService.page(page, wrapper));
    }


    /**
     * 通过id查询量表因子结果表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(evaluationFactorService.getById(id));
    }

    /**
     * 新增量表因子结果表
     * @param evaluationFactor 量表因子结果表
     * @return R
     */
    @Operation(summary = "新增量表因子结果表" , description = "新增量表因子结果表" )
    @SysLog("新增量表因子结果表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_add')" )
    public R save(@RequestBody EvaluationFactorEntity evaluationFactor) {
        return R.ok(evaluationFactorService.save(evaluationFactor));
    }

    /**
     * 修改量表因子结果表
     * @param evaluationFactor 量表因子结果表
     * @return R
     */
    @Operation(summary = "修改量表因子结果表" , description = "修改量表因子结果表" )
    @SysLog("修改量表因子结果表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_edit')" )
    public R updateById(@RequestBody EvaluationFactorEntity evaluationFactor) {
        return R.ok(evaluationFactorService.updateById(evaluationFactor));
    }

    /**
     * 通过id删除量表因子结果表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除量表因子结果表" , description = "通过id删除量表因子结果表" )
    @SysLog("通过id删除量表因子结果表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(evaluationFactorService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param evaluationFactor 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_export')" )
    public List<EvaluationFactorEntity> export(EvaluationFactorEntity evaluationFactor,Long[] ids) {
        return evaluationFactorService.list(Wrappers.lambdaQuery(evaluationFactor).in(ArrayUtil.isNotEmpty(ids), EvaluationFactorEntity::getId, ids));
    }
}